/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.checkbox 
{
	import flash.display.MovieClip;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class MovieClipCheckboxField extends CheckBoxField
	{
		private var _clip : MovieClip ; 
		private var _frame2IsChecked : Boolean  ;
		
		public function MovieClipCheckboxField( clip : MovieClip, state : Boolean = false, frame2IsChecked : Boolean = true ) 
		{
			_clip = clip ; 
			_clip.buttonMode = true ; 
			_clip.mouseChildren = false ; 
			_clip.addEventListener(MouseEvent.CLICK, _toggle ) ;
			_clip.addEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
			_clip.addEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			
			_frame2IsChecked = frame2IsChecked ; 
			super(state) ; 
		}
		
		override public function dispose():void 
		{
			super.dispose();
			if ( _clip ) 
			{
				_clip.removeEventListener(MouseEvent.CLICK, _toggle ) ;
				_clip.removeEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
				_clip.removeEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			}
			_clip = null ; 
		}
		
		override public function setTabIndex(index:int):int 
		{
			_clip.tabChildren = false ; 
			_clip.tabIndex = index ; 
			return super.setTabIndex(index) ; 
		}
		
		override protected function _repaint():void 
		{
			if ( _frame2IsChecked ) _clip.gotoAndStop( _checked ? 2 : 1 ) ; 
			else _clip.gotoAndStop( _checked ? 1 : 2 ) ; 
		}
	}

}